package com.pearlsoft.webapp.rms.resgantt;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.web.basehandler.AbsBaseQueryHandler;

@QueryHandlerComponent(name = "ResGanttQueryHandler")
public class ResGanttQueryHandler extends AbsBaseQueryHandler {

	@Override
	public void handler(Query query, DataSet ds) throws Exception {
		String startDate = (String) query.get("startDate");
		String endDate = (String) query.get("endDate");
		String sql = "SELECT A.startDate,A.endDate,a.resId AS resId,A.resName,SUM(B.cups)AS cpus,SUM(B.memory)AS 'memory' ,B.memoryunit AS memoryunit ,SUM(B.storage)AS 'storage' ,B.storageunit AS storageunit ,SUM(B.netcards)AS netcards "
				+ "FROM rms_myres A"
				+ " LEFT JOIN rms_myhost B "
				+ "ON a.resId = B.resId "
				+ "WHERE (startDate >=? AND startDate<=?)"
				+ " OR(endDate >=? AND endDate<=?)"
				+ " OR (startDate <=? AND endDate>=?) "
				+ "OR (startDate >=? AND endDate<=?)" + "GROUP BY A.resId  ";
		JdbcTemplate jdbcTemplate = this.getBean("jdbcTemplate",
				JdbcTemplate.class);

		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,
				new Object[] {startDate,endDate,startDate,endDate,startDate,endDate,startDate,endDate});
		for (Iterator<Map<String, Object>> iter = list.iterator(); iter
				.hasNext();) {
			Map<String, Object> map = iter.next();
			ds.newRecord();
			Iterator<?> _iterMap = map.entrySet().iterator();
			while (_iterMap.hasNext()) {
				Map.Entry<String, ?> entry = (Map.Entry) _iterMap.next();
				Object key = entry.getKey();
				Object val = entry.getValue();
				ds.setValue(key.toString(), val);
			}
		}
	}

	@Override
	public void getEntity(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void quickSearch(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void advSearch(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

}
